package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.AwardRecord;
import cn.com.connext.scrm.domain.master.Gift;
import cn.com.connext.scrm.repository.master.AwardRecordRepository;
import cn.com.connext.scrm.repository.master.GiftRepository;
import cn.com.connext.scrm.service.dto.GiftDTO;
import cn.com.connext.scrm.service.dto.MemberDTO;
import cn.com.connext.scrm.service.mapper.GiftMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

/**
 * @program: organization-service
 * @author: shawn
 * @create: 2019-06-14 17:52
 **/
@Service
@Transactional
public class GiftService {

    @Autowired
    private GiftRepository giftRepository;

    @Autowired
    private GiftMapper giftMapper;

    @Autowired
    private AwardRecordRepository awardRecordRepository;

    public List<GiftDTO> list() {
        List<Gift> list = giftRepository.findEnabldAll();
        return giftMapper.toDto(list);
    }

    public void bindind(String unionId) {
        List<Gift> list = giftRepository.findEnabldAll();
        Date date = new Date();
        for (Gift gift : list){
            if (gift.getSendTimeStart().before(date) && gift.getSendTimeEnd().after(date)){
                AwardRecord record = new AwardRecord();
                record.setActivityId(gift.getActivityId());
                record.setCardId(gift.getCardId());
                record.setGiftId(gift.getId());
                record.setCreateTime(new Date());
                record.setRegisterTime(ZonedDateTime.now());
                record.setUnionId(unionId);
                record.setState(false);
                record.setAwardContent("-");
                record.setAwardType(gift.getType());
                awardRecordRepository.save(record);
            }
        }
    }
}
